package term

import (
	"context"
	"net/http"

	"mall/service/backend/api/internal/logic/media/term"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/errorx"

	"github.com/zeromicro/go-zero/rest/httpx"
)

// swagger:route post /media/term/list term GetTermList
//
// Get Term list | 获取Term列表
//
// Get Term list | 获取Term列表
//
// Parameters:
//  + name: body
//    require: true
//    in: body
//    type: MediaTermListReq
//
// Responses:
//  200: MediaTermListResp

func GetTermListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), "lang", r.Header.Get("Accept-Language"))
		var req types.MediaTermListReq
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(ctx, w, err)
			return
		}

		if errMsg := svcCtx.Validator.Validate(&req, r.Header.Get("Accept-Language")); errMsg != "" {
			httpx.ErrorCtx(ctx, w, errorx.NewCodeError(svcCtx.Validator.ErrorCode, errMsg))
			return
		}

		l := term.NewGetTermListLogic(ctx, svcCtx)
		resp, err := l.GetTermList(&req)
		if err != nil {
			err = svcCtx.Trans.TransError(ctx, err)
			httpx.ErrorCtx(ctx, w, err)
		} else {
			httpx.OkJsonCtx(ctx, w, resp)
		}
	}
}
